<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>在线聊天</title>
    <script src="http://47.106.66.89:8080/js/vue.min.js"></script>
    <script src="http://47.106.66.89:8080/js/axios.min.js"></script>
    <link href="http://47.106.66.89:8080/js/ele-2.15.7/theme-chalk/index.css" rel="stylesheet">
    <script src="http://47.106.66.89:8080/js/ele-2.15.7/index.js"></script>
    <script src="http://47.106.66.89:8080/js/ele-2.15.7/index.js"></script>
    <script src="sgin.js"></script>
</head>
<body>
<div id="app" style="height: 100%">
    <header class="p900 flexbtw">
        <h3>在线聊天、在线客服</h3>
        <div>
            <sgin ref="a" @login="autoLogin" style="display: flex;gap: 10px">
                <!--                        <template #logined="props">-->
                <template #logined>
                    <el-avatar :src="$refs.a != null?$refs.a.myinfo.head:''"></el-avatar>
                    <el-link href="sgin/logout">退出</el-link>
                </template>
                <template #nologin>
                    <el-link href="register.html">注册</el-link>
                    <el-button type="text"@click="$refs.a.showForget=true">忘记密码</el-button>
                    <el-link href="login.html">登录</el-link>
                </template>
            </sgin>
        </div>
    </header>
    <main class="m900 flexbtw" style="height: calc(100% - 75px)">
        <aside>
            <div v-for="o in onlines">{{o}}</div>
        </aside>
        <article>
            <div id="msgs">
                <div v-for="m in msgs" :class="m.user.id == id ? 'send':'recv'">
                    <label class="user">
                        <img :src="m.user.head" style="width: 50px;height: 50px;object-fit: cover">
                        {{m.user.name}}
                    </label>
                    <img v-if="m.msg.indexOf('file:') == 0" :src="m.msg.substring(5)"
                    style="max-height: 100px;max-width: 200px;object-fit: cover"></img>
                    <span v-else class="msg">{{m.msg}}</span>
                </div>
            </div>
            <textarea placeholder="Ctrl + Center 发送" id="msg"v-model="msg" @paste="sendImage"
                      :readonly="!logined"></textarea>
            <div id="ops">
                <input placeholder="用户名" v-model="id" :disabled="!logined">
                <button @click="login" :disabled="!logined">登录</button>
                <button @click="send" :disabled="!logined">发送</button>
            </div>
        </article>
    </main>
    <footer>源辰公司</footer>
</div>
<script>
    var v = new Vue({
        el:"#app",
        data:{
            ws:null,
            id:"",
            msg:"",
            onlines:[],
            msgs:[],
            logined:false
        },
        methods:{
            login(){
                if(!this.id){
                    this.$alert("亲输入用户名");
                    return;
                }
                this.ws = new WebSocket("ws://localhost:8080/talk/" + this.id);
                this.ws.onopen = ()=>{
                    console.info("服务器连接成功！");
                    this.logined = true;
                }
                this.ws.onclose = ()=>{
                    console.info("下线");
                }
                this.ws.onmessage = (data)=>{
                    let msginfo = JSON.parse(data.data);
                    if (msginfo.onlines){
                        this.onlines = msginfo.onlines;
                    }
                    this.msgs.push({user:msginfo.senUser,msg:msginfo.message});
                    console.info("服务器连接成功！",data);
                }
            },
            send(){
                if(!this.msg){
                    return;
                }
                this.ws.send(this.msg);
                this.msg = "";
            },
            sendImage(e){
                var clipboard = e.clipboardData;
                if (clipboard.items[0].type.match(/image/)) {
                    let fd = new FormData();
                    let blob = clipboard.items[0].getAsFile();
                    let nm = "image.jpg"
                    fd.append("file",blob,nm);
                    fd.append("webPath","/upload/img/");
                    axios.post("upload",fd).then(res=>{
                        if(res.data.code==1){
                            this.ws.send("file:" + res.data.data);
                        }
                    });
                }
            },
            autoLogin(myinfo){
                if(myinfo!=null){
                    this.id = myinfo.id;
                    this.login();
                }
            }
        },
        created(){

        }
    })
</script>
</body>
</html>



















































<style>
    * {
        padding: 0px;
        margin: 0px;
        font-family: "Arial", "Microsoft YaHei", "黑体", "宋体", sans-serif;
    }

    html,body{
        height: 100%;
    }
    body{
        display: flex;
        flex-direction: column;
    }

    a {
        text-decoration: none;
    }

    button{
        padding: 3px 10px;
    }

    .p900 {
        padding: 0px calc((100% - 900px) / 2);
    }

    .m900 {
        width: 900px;
        margin: 0px auto;
    }

    .flexbtw {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    header {
        background-color: #eee;
        flex: none;
    }
    header>h3{
        margin: 10px 0px;
    }

    main {
        flex: auto;
        background-color: #ddd;
    }

    footer {
        flex: none;
        padding: 5px;
        text-align: center;
        background-color: #eee;
    }

    aside {
        width: 200px;
        height: 100%;
        background-color: #ccc;
        flex: none;
    }

    article {
        height: 100%;
        flex: auto;
    }

    article {
        display: flex;
        flex-direction: column;
    }

    #msgs {
        flex: auto;
        box-sizing: border-box;
        padding: 10px;
    }

    #msg {
        flex: none;
        width: 100%;
        height: 100px;
        box-sizing: border-box;
    }

    .send > * {
        float: right;
    }

    .recv > * {
        float: left;
    }

    .send, .recv {
        clear: both;
        padding: 5px 0px;
    }

    .user, .msg {
        padding: 5px;
    }

    .msg {
        background-color: #e9e9e9;
        border-radius: 3px;
    }

    aside>div{
        text-align: center;
        padding: 10px;
        margin: 3px;
        background-color:#8c939d;
    }

    aside>div:hover{
        background-color:#8c939d55;
        cursor: pointer;
    }

    #ops{
        text-align: right;
        padding: 7px;
    }
    #ops>*{
        box-sizing: border-box;
        height: 25px;
        vertical-align: middle;
        margin: 0px 5px;
        text-align: center;
    }
    textarea{
        padding: 3px;
    }
</style>